<?php
class Business_RealEstate{
	
	public static function get($params){
		
	}

    public static function getAllId(){
        try {
            $db = Core_Global::getDatabase();
            $stmt = $db->prepare('select realEstateID from realEstate');
            $stmt->execute();
            $result = $stmt->fetchAll();
            return $result;
        } catch (Zend_Exception $e) {

            return array();
        }
    }

    public  static  function getAllRealEstateByIds($ids){
        try {
            $db = Core_Global::getDatabase();
            $stmt = $db->prepare('CALL sp_getRealEstateByIds(:p_ids)');
            $stmt->bindValue('p_ids',$ids);
            $stmt->execute();
            $result = $stmt->fetchAll();
            return $result;
        } catch (Zend_Exception $e) {
            return $e->getMessage();
        }
    }

	public static function find($params){
		
	}

	public static function create($params){
        try{
            $db = Core_Global::getDatabase();

            $stmt = $db->prepare('CALL sp_insertRealEstate(
                :s_CustomerID,
                :s_RealCategoryId,
                :s_SaleCategoryId,
                :s_LocationId,
                :s_Title,
                :s_Description,
                :s_PriceVND,
                :s_Contact,
                :s_SaleStatus,
                :s_ExpireDate,
                :s_RealArea,
                :s_UseArea,
                :s_Priority,
                :s_RealWidth,
                :s_RealLength,
                :s_Forensic,
                :s_WC,
                :s_BedRoom,
                :s_SittingRoom,
                :s_OtherRoom,
                :s_Alley,
                :s_Position,
                :s_isCarPark,
                :s_isOffice,
                :s_isBusiness,
                :s_isForLive,
                :s_UserId,
                :s_Status,
                :s_CreatedDate,
                :s_UpdatedDate,
                :s_CreatedBy,
                :s_UpdatedBy)');
                $stmt->bindValue('s_CustomerID',$params['CustomerID']);
                $stmt->bindValue('s_RealCategoryId',$params['RealCategoryID']);
                $stmt->bindValue('s_SaleCategoryId',$params['SaleCategoryID']);
                $stmt->bindValue('s_LocationId',$params['LocationID']);
                $stmt->bindValue('s_Title',$params['Title']);
                $stmt->bindValue('s_Description',$params['Description']);
                $stmt->bindValue('s_PriceVND',$params['PriceVND']);
                $stmt->bindValue('s_Contact',$params['Contact']);
                $stmt->bindValue('s_SaleStatus',$params['SaleStatus']);
                $stmt->bindValue('s_ExpireDate',$params['ExpireDate']);
                $stmt->bindValue('s_RealArea',$params['RealArea']);
                $stmt->bindValue('s_UseArea',$params['UseArea']);
                $stmt->bindValue('s_Priority',$params['Priority']);
                $stmt->bindValue('s_RealWidth',$params['RealWidth']);
                $stmt->bindValue('s_RealLength',$params['RealLength']);
                $stmt->bindValue('s_Forensic',$params['Forensic']);
                $stmt->bindValue('s_WC',$params['WC']);
                $stmt->bindValue('s_BedRoom',$params['BedRoom']);
                $stmt->bindValue('s_SittingRoom',$params['SittingRoom']);
                $stmt->bindValue('s_OtherRoom',$params['OtherRoom']);
                $stmt->bindValue('s_Alley',$params['Alley']);
                $stmt->bindValue('s_Position',$params['Position']);
                $stmt->bindValue('s_isCarPark',$params['isCarPark']);
                $stmt->bindValue('s_isOffice',$params['isOffice']);
                $stmt->bindValue('s_isBusiness',$params['isBusiness']);
                $stmt->bindValue('s_isForLive',$params['isForLive']);
                $stmt->bindValue('s_UserId',$params['UserId']);
                $stmt->bindValue('s_Status',$params['Status']);
                $stmt->bindValue('s_CreatedDate',time());
                $stmt->bindValue('s_UpdatedDate',0);
                $stmt->bindValue('s_CreatedBy','admin');
                $stmt->bindValue('s_UpdatedBy','');
            $stmt->execute();
            $result = $stmt->fetch();
            return $result['result'];
        }catch (Zend_Exception $e){
            return $e->getMessage();
        }
    }
	/**
	 * 
	 * @param unknown_type $params
	 * @return number
	 */
	public static function update($params){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_insertRealEstate(:s_RealId,
:s_CustomerID,
:s_RealCategoryId,
:s_SaleCategoryId,
:s_LocationId,
:s_Title,
:s_Description,
:s_PriceVND,
:s_Contact,
:s_SaleStatus,
:s_ExprireDate,
:s_RealArea,
:s_UseArea,
:s_Priority,
:s_RealWidth,
:s_RealLength,
:s_Forensic,
:s_WC,
:s_BedRoom,
:s_SittingRoom,
:s_OtherRoom,
:s_Alley,
:s_Position,
:s_isCarPart,
:s_isOffice,
:s_isBusiness,
:s_isForLive,
:s_UserId,
:s_Status,
:s_CreateDate,
:s_UpdatedDate,
:s_CreatedBy,
:s_UpdatedBy)');
		foreach($params as $key=>$val){
				$stmt->bindValue('s_'.$key,$val);
			}	
			$stmt->execute();			
			
			$result = $stmt->rowCount();
			
			unset($stmt);
			
			return $result;
		} catch (Zend_Exception $e) {
			return 0;
		}
	}
	
	public static function delete($RealIds){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare("CALL sp_deleteRealEstatebyIds(:s_RealId)");
			$stmt->bindValue('s_RealId',$RealIds);
			$stmt->execute();
			return $stmt->rowCount();
		} catch (Zend_Exception $e) {
			return $e->getMessage();
		}
	}
}